﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using Mengsk.ERP.Database;

namespace Mengsk.ERP.App.ArrvieInDelivery
{
    /// <summary>
    /// ArrvieInDelivery.xaml 的交互逻辑
    /// </summary>
    public partial class ArrvieInDeliveryUserControl : UserControl, IUIPlugin
    {
        private ObservableCollection<ArrvieInDeliveryViewModel> deliveryViewModels = new ObservableCollection<ArrvieInDeliveryViewModel>();

        public ArrvieInDeliveryUserControl()
        {
            InitializeComponent();
        }

        public string Title
        {
            get { return "运单登记"; }
        }

        public Control UIControl
        {
            get { return this; }
        }

        private void tbDeliveryNumber_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key != Key.Enter)
            {
                return;
            }
            e.Handled = true;

            string deliveryCompany = this.cbbDeliveryCompanies.Text.Trim();
            string deliveryNumber = this.tbDeliveryNumber.Text.Trim();

            if (string.IsNullOrWhiteSpace(deliveryNumber))
            {
                return;
            }

            if (deliveryCompany == "宅急送" && deliveryNumber.Contains("-"))
            {
                var items = deliveryNumber.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
                int index = 0, max = -1;
                for (int i = 0; i < items.Length; i++)
                {
                    if (max < items[i].Length)
                    {
                        max = items[i].Length;
                        index = i;
                    }
                }
                deliveryNumber = items[index];
            }

            //Delivery.ArrvieInDelivery delivery = new Delivery.ArrvieInDelivery
            //{
            //    DeliveryCompany = deliveryCompany,
            //    DeliveryNumber = deliveryNumber,
            //    ArriveReason = "",
            //    Comment = "",
            //    CreateTime = DateTime.Now,
            //};

            this.deliveryViewModels.Add(new ArrvieInDeliveryViewModel
            {
                ArriveInReason = this.cbbArriveReason.Text.Trim(),
                DeliveryCompany = this.cbbDeliveryCompanies.Text.Trim(),
                DeliveryNumber = deliveryNumber
            });
            this.tbDeliveryNumber.Text = "";
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.dgvDelivery.ItemsSource = this.deliveryViewModels;
            this.deliveryViewModels.CollectionChanged += deliveryViewModels_CollectionChanged;
        }

        void deliveryViewModels_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            this.tbCount.Text = string.Format("当前共 {0} 条快递记录", this.deliveryViewModels.Count);
        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            this.deliveryViewModels.Clear();
        }

        private void tbDeliveryNumber_GotFocus(object sender, RoutedEventArgs e)
        {
            this.Dispatcher.BeginInvoke(new Action(() => this.tbDeliveryNumber.SelectAll()));
        }

        private void btnMakeReturn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var deliverys = this.deliveryViewModels.Select(obj => obj.ToArriveInDelivery()).ToArray();

                foreach (var delivery in deliverys)
                {
                    try
                    {
                        DBHelper.Instance.Insert("", "ArrvieInDelivery", delivery, false);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, delivery.DeliveryCompany + "  " + delivery.DeliveryNumber);
                        break;
                    }
                }

                MessageBox.Show("已更新完成");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnImport_Click(object sender, RoutedEventArgs e)
        {

        }

        private void DeleteOrder_Click(object sender, RoutedEventArgs e)
        {
            ArrvieInDeliveryViewModel order = (e.Source as Hyperlink).DataContext as ArrvieInDeliveryViewModel;
            if (order == null)
            {
                return;
            }

            this.deliveryViewModels.Remove(order);
        }
    }
}
